package app;
import info.clearthought.layout.TableLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Code extends javax.swing.JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	private JTextPane textInput;
	private JButton cancel;
	private JButton ok;
	public Main main;
	
	
	 @SuppressWarnings("unused")
	private static final String ACTION_CLOSE = "ACTION_CLOSE";
	 private JScrollPane jScrollPane1;
	 KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
	/*
	 *
	 * 1: imagen
	 * 2: youtube
	 * 3: google video
	 * 4: goear
	 * 5: swf
	 * 6: megavideo
	 * 7: link
	 * 8: bliptv
	 * 9: quote
	 * 10: vimeo
	 * 11: DailyMotion
	 * 12: Metacafe
	 */

	/**
	* Auto-generated main method to display this JDialog
	*/
		
	public Code(JFrame frame) {
		super(frame);
		initGUI();
		addEscapeKey ();
	}
	
	
	
	
	
	
	
	private void addEscapeKey ()
	{
	// Handle escape key to close the dialog

	KeyStroke escape = KeyStroke.getKeyStroke (KeyEvent.VK_ESCAPE, 0, false);
	Action escapeAction = new AbstractAction ()
	{
	/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

	public void actionPerformed (ActionEvent e)
	{
		cancelActionPerformed(e);
	
	}
	};

	getRootPane ().getInputMap (JComponent.WHEN_IN_FOCUSED_WINDOW).put (escape, "ESCAPE");
	getRootPane ().getActionMap ().put ("ESCAPE", escapeAction);
	}

	
	
	public void obtenerFoco() {
		textInput.requestFocus();
	}
	
	
	
	public void conectar(Main a1){
		main=a1;
	}
	
	
	private void initGUI() {
		try {
			
			this.setIconImage(new ImageIcon(getClass().getClassLoader().getResource("app/postinga16x16.png")).getImage());
			TableLayout thisLayout = new TableLayout(new double[][] {{7.0, TableLayout.FILL, TableLayout.FILL, 7.0, 7.0}, {27.0, TableLayout.FILL, TableLayout.FILL, TableLayout.FILL, 149.0, TableLayout.FILL, 7.0}});
			thisLayout.setHGap(5);
			thisLayout.setVGap(5);
			getContentPane().setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(432, 385));
			getContentPane().setBackground(new java.awt.Color(255,255,255));
			this.setLocation(new java.awt.Point(100, 24));
			this.setTitle("Insertar Codigo");
			this.setAlwaysOnTop(true);
			
			{
				ok = new JButton();
				getContentPane().add(ok, "1, 5, r, f");
				
				ok.setText("Aceptar");
				ok.setFont(new java.awt.Font("Ubuntu",0,12));
				ok.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						okActionPerformed(evt);
					}
				});

				ok.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						okMouseClicked(evt);
					}
				});
			}
			{
				cancel = new JButton();
				getContentPane().add(cancel, "2, 5, l, f");
				cancel.setText("Cancelar");
				
				cancel.setFont(new java.awt.Font("Ubuntu",0,12));
				cancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						cancelActionPerformed(evt);
					}
				});
			}

			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1, "1, 1, 3, 4");
				{
					textInput = new JTextPane();
					jScrollPane1.setViewportView(textInput);
					textInput.setDragEnabled(true);
				}
			}
			this.setSize(432, 385);
			getPopup1();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
		
	
	
	private void okMouseClicked(MouseEvent evt) {
	okAction();
		
	}
	private void okAction() {
		
		main.generarCode(textInput.getText());
		main.obtenerFoco();
		this.dispose();
		textInput.setText("");
	}
	
	
	
	/********************************PopUp*******************************************/
	JPopupMenu popup1; 
	JMenuItem PopCortar1;
	JMenuItem PopPegar1;
	JMenuItem PopCopiar1;
	JMenuItem PopSelAll1;
	private String icon_copy="copy.gif";
	private String icon_paste="paste.gif";
	private String icon_cut="cut.gif";
	private String icon_select_all="selectall.gif";
	
	
	private JMenuItem getPopCortar1() {
		if(PopCortar1 == null) {
			PopCortar1 = new JMenuItem("Cortar");
			PopCortar1.setIcon(new ImageIcon(getClass().getResource("cut.gif")));
			PopCortar1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopCortar1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopCortar1ActionPerformed(evt);
				}
			});
		}
			return PopCortar1;
		}
	
	private JMenuItem getPopCopiar1() {
		if(PopCopiar1 == null) {
			PopCopiar1 = new JMenuItem("Copiar");
			PopCopiar1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopCopiar1.setIcon(new ImageIcon(getClass().getResource("copy.gif")));
			PopCopiar1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopCopiar1ActionPerformed(evt);
				}
			});
		}
			return PopCopiar1;
		}
			
			
	private JMenuItem getPopPegar1() {
		if(PopPegar1 == null) {
			PopPegar1 = new JMenuItem("Pegar");
			PopPegar1.setIcon(new ImageIcon(getClass().getResource("paste.gif")));
			PopPegar1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopPegar1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopPegar1ActionPerformed(evt);
				}
			});
		}
			return PopPegar1;
		}
	
	
	
	private JMenuItem getPopSelAll1() {
		if(PopSelAll1 == null) {
			PopSelAll1 = new JMenuItem("Seleccionar todo");
			PopSelAll1.setIcon(new ImageIcon(getClass().getResource(icon_select_all)));
			PopSelAll1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopSelAll1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopSelAll1ActionPerformed(evt);
				}
			});	
		}
			return PopSelAll1;
		}
	
	private void PopPegar1ActionPerformed(ActionEvent evt) {
    	
		textInput.paste();
    		textInput.requestFocus();
    	
    }
    
    private void PopCopiar1ActionPerformed(ActionEvent evt) {
    	
    		textInput.copy();
    	
    }
    
    
    
    private void PopSelAll1ActionPerformed(ActionEvent evt) {
    
    					textInput.selectAll(); 
    			    	textInput.requestFocus();		    	
    }
    
	
    private void PopCortar1ActionPerformed(ActionEvent evt) {
    	
    	textInput.cut();
    	textInput.requestFocus();
    	
    	
	}
	
    private JPopupMenu getPopup1() {
		if(popup1 == null) {
			popup1 = new JPopupMenu();
			textInput.setComponentPopupMenu (popup1);
			popup1.add(getPopCortar1());
			popup1.add(getPopCopiar1());
			popup1.add(getPopPegar1());
			popup1.add(getPopSelAll1());
			
		}
		return popup1;
		}
	
	
    /********************************PopUp*******************************************/
	
	
	/**
	* Auto-generated method for setting the popup menu for a component
	*/
	
	private void pegarActionPerformed(ActionEvent evt) {
		textInput.paste();
	}
	
	private void okActionPerformed(ActionEvent evt) {
		okAction();
		
	}
	
	private void cancelActionPerformed(ActionEvent evt) {
		textInput.setText("");
		main.obtenerFoco();
		this.dispose();
	}

}
